package com.jeewms.www.tms.ui.acitivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.jeewms.www.tms.R;
import com.jeewms.www.tms.base.BaseBackActivity;
import com.jeewms.www.tms.bean.vm.Ginfo;
import com.jeewms.www.tms.bean.vm.wmapiUtil.ResultDO;
import com.jeewms.www.tms.constance.Constance;
import com.jeewms.www.tms.util.GsonUtils;
import com.jeewms.www.tms.util.Logutil;
import com.jeewms.www.tms.util.SharedPreferencesUtil;
import com.jeewms.www.tms.util.UUIDUtil;
import com.jeewms.www.tms.util.z_toast;
import com.jeewms.www.tms.volley.HTTPUtils;
import com.jeewms.www.tms.volley.VolleyListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Resum_Activity extends BaseBackActivity {

    private TextView btn = null;
    private TextView tv_btnbc=null;
    private long lastClickTime = 0L;
    private static final int FAST_CLICK_DELAY_TIME = 1000;  // 快速点击间隔
    String tag ;
    ListView mLvGent;
    ListView trlistview;
    List<Ginfo> mginfo;
    GINfo madaper;
    GINfo MyAdapter;
    private int titlebar_back;
    public static void show(Context context) {
        Intent intent = new Intent(context, Resum_Activity.class);
        context.startActivity(intent);

    }

    protected void initView() {
        super.initView();

        Logutil.print("测试System的使用LOGCAT");
        setTitle("预留上架");
        final EditText et_fangweima = (EditText) findViewById(R.id.et_fangweima);
        final EditText tv_werks = findViewById(R.id.et_werks);//工厂
        final EditText et_werks = (EditText) findViewById(R.id.et_werks);
        final EditText et_lgort = (EditText) findViewById(R.id.et_lgort);

        et_fangweima.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
         et_fangweima.setOnKeyListener(new View.OnKeyListener() {
             @Override
             public boolean onKey(View v, int i, KeyEvent keyEvent) {
                 Logutil.print("keycode " + keyEvent.getKeyCode());
                 z_toast t_dm = new z_toast();
                 if (i == KeyEvent.KEYCODE_ENTER && keyEvent.getAction() == KeyEvent.ACTION_UP ) {
                     //  t_dm.showToast("保存中。。。",ShangjiaAcitivity.this);
                     if (TextUtils.isEmpty(et_fangweima.getText().toString())) {
                         t_dm.showToast("预留号不能为空！",Resum_Activity.this);
                         return false;
                     }
                     final int LONG_DELAY = 1000;
                     final EditText et_fangweima = (EditText) findViewById(R.id.et_fangweima);
                     getData();
                   //  et_fangweima.setText("");
                     //执行清空动作 2018/09/29
                     return true;
                 }


                 return false;
             }
         });







        btn = (TextView) findViewById(R.id.tv_search);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(System.currentTimeMillis() - lastClickTime < FAST_CLICK_DELAY_TIME){
                    Logutil.print("多次速度调用接口");
                    return;
                }
                lastClickTime = System.currentTimeMillis();
                z_toast ts = new z_toast();
                if (TextUtils.isEmpty(et_fangweima.getText().toString())) {
                    ts.showToast("预留号不能为空！",Resum_Activity.this);
                    return;
                }
//                if (TextUtils.isEmpty(et_lgort.getText().toString())) {
//                    ts.showToast("库位不能为空！",Resum_Activity.this);
//                    return;
//                }
                getData();
            }
        });

        //  Logutil.print("执行出问题");
    }
    private void getData() {
//        String url = Constance.gettSapLsqdControllerURL() + "?username=" + SharedPreferencesUtil.getInstance(this).getKeyValue(Constance.SHAREP.LOGINNAME);
//        if (!StringUtil.isEmpty(etJiaojiedan.getText().toString()))
//            url += "&searchstr=" + etJiaojiedan.getText().toString();
//        if (!StringUtil.isEmpty(etFangweima.getText().toString()))
//            url += "&searchstr2=" + etFangweima.getText().toString();

        // String url = "http://192.168.0.108:8080/zzwms/rest/wmapi/getdoc/KCCX";
        //  String url = "http://172.17.220.244:8080/zzwms/rest/wmapi/getdoc/KCCX";
        //   String url = "http://192.168.200.100/zzwms/rest/wmapi/getdoc/KCCX";
        final EditText et_fangweima1 = (EditText) findViewById(R.id.et_fangweima);
        final EditText et_werks1 = (EditText) findViewById(R.id.et_werks);
        final EditText et_lgort1 = (EditText) findViewById(R.id.et_lgort);
        final EditText et_chuwei1 = (EditText) findViewById(R.id.et_chuwei);
        final EditText et_fwm = (EditText) findViewById(R.id.et_fwm);
        String url = "http://192.168.200.100/zzwms/rest/wmapi/getdoc/YLYC?docid="+et_fangweima1.getText().toString()+"&searchstr2="+et_fwm.getText().toString();
      //  String url = "http://172.17.210.197:8080/zzwms/rest/wmapi/getdoc/KCCX";
//        String url = "http://172.17.210.197:8080/zzwms/rest/wmapi/getdoc/KCCX"+"?searchstr1="
//                +"&searchstr2="  + et_fangweima1.getText().toString()  +"&searchstr3="  + et_chuwei1.getText().toString()  +"&searchstr4="  + et_werks1.getText().toString()
//                +"&searchstr5="  + et_lgort1.getText().toString();
        Logutil.print("url" + url);
        HTTPUtils.get(getActivity(), url, new VolleyListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }

            @Override
            public void onResponse(String response) {
                Logutil.print("res==" + response);
                ResultDO vm = GsonUtils.parseJSON(response, ResultDO.class);
                mginfo = new ArrayList<Ginfo>();
                // Logutil.print("mginfo.size()1=");
                for(int i = 0;i<vm.getObj().size();i++) {
                    Ginfo bean = new Ginfo();
                    bean.setResid(vm.getObj().get(i).getWmX2());//物料号
                    bean.setMaktx(vm.getObj().get(i).getWmX4());//物料描述
                    bean.setBinId(vm.getObj().get(i).getWmX6());//原始库位
                    bean.setMenge(vm.getObj().get(i).getWmX17());//良品数量
                    bean.setMblgort(vm.getObj().get(i).getWmX13());//良品目标库位.
                    bean.setMbcw(vm.getObj().get(i).getWmX9());//良品目标仓位
                    bean.setBllgort("B901");
                    bean.setYldh(vm.getObj().get(i).getWmX1());//预留单号
                    bean.setYldhh(vm.getObj().get(i).getWmX14());//预留单行项目
                 //   bean.set

                    //   bean.setMenge(Integer.parseInt(vm.getObj().get(i).getWmX3()));
                //    bean.setBinId(vm.getObj().get(i).getWmX6());
                  //  bean.setMaktx(vm.getObj().get(i).getWmX8());






                    mginfo.add(bean);
                        Logutil.print("mginfo.size()2="+mginfo.size());
                }
                initview();

                //    Logutil.print("mginfo.size()3="+mginfo.size());

            }
        });

    }

    @SuppressLint("WrongViewCast")
    private void initview() {
        mLvGent = (ListView)findViewById(R.id.trlistview);
        madaper = new GINfo();
        mLvGent.setAdapter(madaper);

    }
    class GINfo extends BaseAdapter {
        @Override
        public int getCount() {
            return mginfo.size();
        }

        @Override
        public Ginfo getItem(int i) {

            return mginfo.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            //---------------------------------------------------------------------------------------------------------变量定义
            final int k = 0;
            final View layout = View.inflate(Resum_Activity.this, R.layout.item_resum,null); //取ITEM的值
            final View layout2 = View.inflate(Resum_Activity.this, R.layout.item_resum,null);//取表头的值
            //---------------------------------------------------------------------------------------------------------变量定义


            //------------------------------------------------------------------------------------------------------------ ----从页面能获取数据
            TextView tv_btnbc = layout.findViewById(R.id.tv_btnbc);// 设置LISTVIEW监听按钮

            tv_btnbc.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    TextView tv_yld = layout.findViewById(R.id.tv_yld);//预留单号
                    TextView tvmatnr = layout.findViewById(R.id.tv_matnr);//物料号
                    TextView tvmaktx = layout.findViewById(R.id.tv_maktx);//物料描述
                    TextView tv_lgort = layout.findViewById(R.id.tv_lgort);//原始库位
                    TextView tvmenge = layout.findViewById(R.id.tv_menge);//良品数量
                    TextView et_lplgort = layout.findViewById(R.id.et_lplgort);//良品目标库位
                    TextView et_mbcw = layout.findViewById(R.id.et_mbcw);//良品目标仓位
                    TextView et_blmbsl = layout.findViewById(R.id.et_blmbsl);//不良目标数量
                    TextView et_blslgort = layout.findViewById(R.id.et_blslgort);//不良目标库位
                    TextView tv_yldhh = layout.findViewById(R.id.tv_yldhh);//预留单行号

                    z_toast ts = new z_toast();

                    //-------------------------------------------------------------------------------------------------------------调用接口，给接口JSon赋值
                    Map<String, String> map = new HashMap<>();
                    //   map.put("createBy",SharedPreferencesUtil.getInstance(mContext).getKeyValue(Constance.SHAREP.LOGINNAME));
                    map.put("wxs1", UUIDUtil.getUUID32());//流水唯一ID号
                    map.put("createBy", SharedPreferencesUtil.getInstance(getActivity()).getKeyValue(Constance.SHAREP.LOGINNAME));
                    map.put("wxs4", tvmatnr.getText().toString());//物料号
                    map.put("wxs3", tv_yld.getText().toString());//预留号
                    map.put("wxs9", tv_lgort.getText().toString());//原始库位
                    map.put("wxs6", tvmenge.getText().toString());//良品数量
                    map.put("wxs10", et_lplgort.getText().toString());//良品目标库位
                    map.put("wxs5", et_mbcw.getText().toString());//良品目标仓位
                    map.put("wxs7", et_blmbsl.getText().toString());//不良数量
                    map.put("wxs11", et_blslgort.getText().toString());//不良库位
                    map.put("wxs41", tv_yld.getText().toString());//雷射订单号 1
                    map.put("wxs42", tvmatnr.getText().toString());//物料号
                    map.put("wxs43", tv_yldhh.getText().toString());//预留行项目
                    //map.put("x2", SharedPreferencesUtil.getInstance(mContext).getKeyValue(Constance.SHAREP.LOGINNAME)); //登录用户名
                    final JSONObject jsonObject = new JSONObject(map);
                    final Map<String, String> params = new HashMap<>();
                    params.put("tSapLtttstr", jsonObject.toString());//上传实体json
                  //  String url = "http://172.17.210.197:8080/zzwms/rest/wmapi/getdoc/YLYC?docid=395202";
                    // String url = "http://192.168.200.100:8080/zzwms/rest/wmapi/docltcreate"+ "/YK";
                    //    String Zurl = "http://172.17.210.196:8080/zzwms/rest/wmapi/docltcreate?doctype=YK&tSapLtttstr="+ params.put("tSapLtttstr", jsonObject.toString())+"";
                    String Zurl = "http://192.168.200.100/zzwms/rest/wmapi/docltcreate?doctype=YLYC&tSapLtttstr="+ params.put("tSapLtttstr", URLEncoder.encode(jsonObject.toString()))+"";
                    Logutil.print("url" + Zurl);
                    ts = new z_toast();
                    ts.showToast("保存中，请稍后。。。。",Resum_Activity.this);
                    z_toast  tk = new z_toast();
                    HTTPUtils.get(getActivity(), Zurl, new VolleyListener() {

                        public void onErrorResponse(VolleyError error) {
                            z_toast  tp = new z_toast();
                            getData();
                            tp.showToast("jeecg数据为空",Resum_Activity.this);
                        }

                        @Override
                        public void onResponse(String response) {
                            // -----------------------------------------------------------------------------------------取json 返回数据并显示
                            ResultDO result = GsonUtils.parseJSON(response, ResultDO.class);
                            z_toast  tk = new z_toast();
                            tk.showToast(result.getErrorMsg(),Resum_Activity.this);
                            Logutil.print("返回成功!");
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            //重新调用库存接口 刷新页面，取得实际库存.
                            getData();

                            //重新调用库存接口 刷新页面，取得实际库存.

                        }
                    });

                }
            });

            //多行ITEM的显示，只要在实体类GINfo,java 增加变量,在这里增加变量参数
            TextView tv_yld = layout.findViewById(R.id.tv_yld);//预留单号
            TextView tv_yldhh = layout.findViewById(R.id.tv_yldhh);//预留单行号
            TextView tvmatnr = layout.findViewById(R.id.tv_matnr);//物料号
            TextView tvmaktx = layout.findViewById(R.id.tv_maktx);//物料描述
            TextView tv_lgort = layout.findViewById(R.id.tv_lgort);//原始库位
            TextView tvmenge = layout.findViewById(R.id.tv_menge);//良品数量
            TextView et_lplgort = layout.findViewById(R.id.et_lplgort);//良品目标库位
            TextView et_mbcw = layout.findViewById(R.id.et_mbcw);//良品目标仓位
            TextView et_blmbsl = layout.findViewById(R.id.et_blmbsl);//不良目标数量
            TextView et_blslgort = layout.findViewById(R.id.et_blslgort);//不良目标库位
            //  Logutil.print(tvmatnr.getText().toString());
            Ginfo bean = mginfo.get(i);
            tvmatnr.setText(bean.getResid());//物料号
            // tvmenge.setText(Integer.toString(bean.getMenge()));
            tvmenge.setText(bean.getMenge());///良品数量
            tvmaktx.setText(bean.getMaktx());//物料描述
            tv_yld.setText(bean.getYldh());//预留单号
            tv_yldhh.setText(bean.getYldhh());//预留单行号
            tv_lgort.setText(bean.getBinId());//原始目标库位
            et_lplgort.setText(bean.getMblgort());//良品目标库位
            et_mbcw.setText(bean.getMbcw());//良品目标仓位
            et_blmbsl.setText(bean.getBlmenge());//不良目标数量
            et_blslgort.setText(bean.getBllgort());//不良目标库位



       //     tvbinId.setText(bean.getBinId());
            return layout;
        }
    }

    private void parseDdta(String  result) throws JSONException {

        JSONObject JSONobject = new JSONObject(result);
        JSONArray JSONArray = JSONobject.getJSONArray("list");

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
    protected int getContentResId() {
        return R.layout.activity_resum;
    }
}
